const path = require("path");
const db = require(path.join(__dirname, "../db"));
const express = require("express");
const router = express.Router();
router.get("/getbooks", (req, res) => {
    db("select * from student", null, (err, result) => {
        if (err) return console.log(err);
        res.send({
            status: 200,
            msg: "获取成功",
            data: result,
        });
    });
});

// 添加书籍的接口
router.post("/addbook", (req, res) => {
    // console.log(req.body);
    db("insert into student set ?", req.body, (err, result) => {
        if (err || result.affectedRows <= 0) {
            console.log(err);
            res.send({ status: 500, msg: "添加失败" });
        } else {
            res.send({ status: 201, msg: "添加成功" });
        }
    });
});

// 删除书籍接口
router.get("/delbook", (req, res) => {
    // 获取客户端的请求参数id
    let id = req.query.id;
    // console.log(id);
    db("delete from student where id=?", id, (err, result) => {
        if (err || result.affectedRows <= 0) {
            console.log(err);
            res.send({ status: 500, msg: "删除失败" });
        } else {
            res.send({ status: 200, msg: "删除成功" });
        }
    });
});

module.exports = router;